home *** CD-ROM | disk | FTP | other *** search
- #include <IV-look/kit.h>
- #include <InterViews/adjust.h>
- #include <InterViews/background.h>
- #include <InterViews/box.h>
- #include <InterViews/glue.h>
- #include <InterViews/label.h>
- #include <InterViews/place.h>
- #include <InterViews/session.h>
- #include <InterViews/style.h>
- #include <InterViews/window.h>
- #include <OS/string.h>
- #include <stdio.h>
-
- class App : public Adjuster {
- public:
- App(Adjustable*);
- virtual ~App();
-
- void print_value();
- void continuous();
-
- virtual void update();
- virtual void disconnect(Adjustable*);
- private:
- Adjustable* adjustable_;
- boolean continuous_;
- };
-
- App::App(Adjustable* a) {
- adjustable_ = a;
- a->attach(Dimension_X, this);
- continuous_ = false;
- }
-
- App::~App() {
- if (adjustable_ != nil) {
- adjustable_->detach(Dimension_X, this);
- }
- }
-
- void App::print_value() {
- printf("%.5f\n", adjustable_->cur_lower(Dimension_X));
- }
-
- void App::continuous() {
- continuous_ = !continuous_;
- }
-
- void App::update() {
- if (continuous_) {
- print_value();
- }
- }
-
- void App::disconnect(Adjustable*) {
- adjustable_ = nil;
- }
-
- declare(ActionCallback,App);
- implement(ActionCallback,App);
-
- class BoundedValue : public Adjustable {
- protected:
- BoundedValue();
- public:
- BoundedValue(Coord lower, Coord upper);
- virtual ~BoundedValue();
-
- virtual void lower_bound(Coord);
- virtual void upper_bound(Coord);
- virtual void current_value(Coord);
- virtual void scroll_incr(Coord);
- virtual void page_incr(Coord);
-
- virtual Coord lower(DimensionName) const;
- virtual Coord upper(DimensionName) const;
- virtual Coord length(DimensionName) const;
- virtual Coord cur_lower(DimensionName) const;
- virtual Coord cur_upper(DimensionName) const;
- virtual Coord cur_length(DimensionName) const;
-
- virtual void scroll_to(DimensionName, Coord position);
- virtual void scroll_forward(DimensionName);
- virtual void scroll_backward(DimensionName);
- virtual void page_forward(DimensionName);
- virtual void page_backward(DimensionName);
- private:
- Coord curvalue_;
- Coord lower_;
- Coord span_;
- Coord scroll_incr_;
- Coord page_incr_;
- };
-
- BoundedValue::BoundedValue() {
- scroll_incr_ = 0.0;
- page_incr_ = 0.0;
- }
-
- BoundedValue::BoundedValue(Coord lower, Coord upper) {
- lower_ = lower;
- span_ = upper - lower;
- scroll_incr_ = span_ * 0.04;
- page_incr_ = span_ * 0.4;
- curvalue_ = (lower + upper) * 0.5;
- }
-
- BoundedValue::~BoundedValue() { }
-
- void BoundedValue::lower_bound(Coord c) { lower_ = c; }
- void BoundedValue::upper_bound(Coord c) { span_ = c - lower_; }
-
- void BoundedValue::current_value(Coord value) {
- curvalue_ = value;
- constrain(Dimension_X, curvalue_);
- notify(Dimension_X);
- notify(Dimension_Y);
- }
-
- void BoundedValue::scroll_incr(Coord c) { scroll_incr_ = c; }
- void BoundedValue::page_incr(Coord c) { page_incr_ = c; }
-
- #define access_function(name,value) \
- Coord BoundedValue::name(DimensionName) const { \
- return value; \
- }
-
- access_function(lower,lower_)
- access_function(upper,lower_ + span_)
- access_function(length,span_)
- access_function(cur_lower,curvalue_)
- access_function(cur_upper,curvalue_)
- access_function(cur_length,0)
-
- void BoundedValue::scroll_to(DimensionName d, Coord position) {
- Coord p = position;
- constrain(d, p);
- if (p != curvalue_) {
- curvalue_ = p;
- notify(Dimension_X);
- notify(Dimension_Y);
- }
- }
-
- #define scroll_function(name,expr) \
- void BoundedValue::name(DimensionName d) { \
- scroll_to(d, curvalue_ + expr); \
- }
-
- scroll_function(scroll_forward,+scroll_incr_)
- scroll_function(scroll_backward,-scroll_incr_)
- scroll_function(page_forward,+page_incr_)
- scroll_function(page_backward,-page_incr_)
-
- int main(int argc, char** argv) {
- Session* session = new Session("Himom", argc, argv);
- Kit* kit = Kit::instance();
- Style* style = session->style();
- BoundedValue* b = new BoundedValue(0.0, 100.0);
- App* a = new App(b);
- b->current_value(50.0);
- b->scroll_incr(5.0);
- b->page_incr(20.0);
- Glyph* print_button = kit->simple_push_button(
- "Print value", style, new ActionCallback(App)(a, &App::print_value)
- );
- Glyph* continuous_button = kit->simple_toggle_button(
- "Continuous", style, new ActionCallback(App)(a, &App::continuous)
- );
- return session->run_window(
- new ApplicationWindow(
- kit->flat_frame(
- new Margin(
- new LRBox(
- new TBBox(
- new Center(print_button, float(0.5), float(1.0)),
- new VGlue(10.0),
- new HCenter(continuous_button, 0.5)
- ),
- kit->inset_frame(kit->vscroll_bar(b, style), style)
- ),
- 5.0
- ),
- style
- )
- )
- );
- }
-